<?php

namespace app\api\controller;

use app\api\model\Account as AccountModel;
use think\facade\Filesystem;


class Account
{
    public $AccountData;

    public function __construct()
    {
        //判断token是否有效，通过token读取学生的所有信息
        $token = input('get.token');
        if (empty($token)) output_error('token为空');
        $this->accountData = AccountModel::where('member_token', '=', $token)->find();
        if (empty($this->accountData)) output_error('token无效');
    }

    //注册功能
    public function getData()
    {
        //输出信息，去除“member_password”字段
        unset($this->accountData['member_password']);
        output_data($this->accountData);
    }

    public function modifyName()
    {
        //修改学生姓名，学生姓名可重复。
        $name = input('get.name');
        if (empty($name)) output_error('新昵称不能为空');
        AccountModel::where('member_id', '=', $this->accountData['member_id'])
            ->update(array('member_name' => $name));
        output_data(1);
    }

    public function modifyAvatar()
    {
        //修改学生头像，需要自行创建文件夹，文件系统。
        //学生头像文件唯一，更新时需要删除原有的文件
        $file = request()->file('image');
        $savename = Filesystem::disk('avatar')
            ->putFile('', $file, 'uniqid');
        if ($this->accountData['member_avatar'] != 'default.png') {
            unlink(app()->getRootPath() . 'public\\upload\\avatar\\'
                . $this->accountData['member_avatar']);
        }
        AccountModel::where('member_id', '=', $this->accountData['member_id'])
            ->update(array('member_avatar' => $savename));
        output_data($savename);
    }

}
